﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Phatent.Model.Functions;
using Phatent.Logic.Functions;
using Phatent.Model.Results;
using Newtonsoft.Json.Linq;
namespace Phatent.Website.Areas.Home.Controllers
{
    [RouteArea("Home")]
    [RoutePrefix("Function")]
    public class FunctionApiController : ApiController
    {
        [Route("EnterMeeting")]
        [HttpPost]
        public JsonEntity<MeetingModel> EnterMeeting(JObject request)
        {
            var errinfo = "进入会议失败，原因：";
            try
            {
                var meeting = MeetingLogic.GetTopModel();
                if (meeting != null)
                {
                    var realname = request.Value<string>("realname");
                    var password = request.Value<string>("password");
                    var userRole = request.Value<string>("userRole");
                    if ((userRole == "host" && password == meeting.AdminPassword) || (userRole=="normal" && password==meeting.JoinPassword))
                    {
                        return new JsonEntity<MeetingModel>(new MeetingModel() {  MeetingNumber=meeting.MeetingNumber});
                    }else
                        errinfo += "密码错误。";
                }
                else
                    errinfo += "会议不存在。";
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
            }
            return new JsonEntity<MeetingModel>(null, errinfo, false);
        }

        [Route("CreateEmployee")]
        [HttpPost]
        public JsonEntity<EmployeeModel> CreateEmployee(JObject request)
        {
            var errinfo = "提交失败，原因：";
            try
            {
                var model = new EmployeeModel();
                model.EmployeeName=request.Value<string>("employeeName");
                model.Post=request.Value<string>("post");
                model.RealName= request.Value<string>("realname");
                model.UnitName = request.Value<string>("unitName");
                model.Mobile = request.Value<string>("mobile");
                model.Email = request.Value<string>("email");
                EmployeeLogic.CreateNew(model);
                try
                {
                    org.hengfeng.www.SmsInterface si = new org.hengfeng.www.SmsInterface();
                    si.Send("10999999", "13806691625", "温馨提醒：有一个大知网的任职查询提交，请您关注！", DateTime.Now);
                }
                catch { }
                return new JsonEntity<EmployeeModel>(model);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
            }
            return new JsonEntity<EmployeeModel>(null, errinfo, false);
        }
    }
}
